package com.cl.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.wsdl.http.UrlEncoded;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cl.bean.Client;
import com.cl.bean.Dept;
import com.cl.bean.Emp;
import com.cl.service.ClientService;
import com.cl.service.DeptService;
import com.cl.service.EmpService;
import com.cl.utils.PageBean;
import com.cl.web.base.BaseAction;

import net.sf.json.JSONObject;

@Controller
@Scope("prototype")
public class EmpAction extends BaseAction<Emp> {
	
	@Autowired
	private EmpService empService;
	
	@Autowired
	private ClientService clientService;
	
	@Autowired
	private DeptService deptService;
	
	private int p_id;
	
	public void setP_id(int p_id) {
		this.p_id = p_id;
	}
	
	public String findincumbencyByPage() throws IOException{
		dc.add(Restrictions.eq("e_is", 1));
		PageBean pb = empService.getPageBean(dc,page,limit);
		ObjectToJson(pb,new String[]{"cutterPage","pageSize","start","dept"});
		return NONE;
	}
	
	public String findRecoverByPage() throws IOException{
		dc.add(Restrictions.eq("e_is", 0));
		PageBean pb = empService.getPageBean(dc,page,limit);
		ObjectToJson(pb,new String[]{"cutterPage","pageSize","start","dept","PName"});
		return NONE;
	}
	
	public String findByPage() throws IOException{
		PageBean pb = empService.getPageBean(dc,page,limit);
		ObjectToJson(pb,new String[]{"cutterPage","pageSize","start","dept"});
		return NONE;
	}
	
	public String add(){
		empService.save(model);
		return LIST;
	}
	
	public String findByPid() throws IOException{
		List<Emp> listEmp = empService.findByPid(p_id);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		PrintWriter pw = response.getWriter();
		JSONObject json = new JSONObject();
		String i = "";
		for (Emp emp : listEmp) {
			List<Client> listClient = clientService.findByEid(emp.getE_id());
			if(listClient.size() > 0){
				i += emp.getE_truename() + " ";
			}
		}
		if(i.length() > 0){
			json.put("result", i);
			pw.println(json.toString());
		}else{
			json.put("result", "ok");
			pw.println(json.toString());
		}
		return NONE;
	}
	
	public String del(){
		empService.del(model.getE_id());
		return LIST;
	}
	
	public String recoveryFindById(){
		Emp emp = empService.findById(model.getE_id());
		ServletActionContext.getRequest().getSession().setAttribute("emp", emp);
		return "recovery";
	}
	
	public String recovery(){
		Emp emp = empService.findById(model.getE_id());
		emp.setE_truename(model.getE_truename());
		Dept dept = deptService.findById(model.getDept().getP_id());
		emp.setDept(dept);
		emp.setE_is(1);
		empService.update(emp);
		return NONE;
	}
	
	public String queryfindByPage() throws IOException{
		if(StringUtils.isNotBlank(model.getE_truename())){
			dc.add(Restrictions.like("e_truename", "%"+model.getE_truename()+"%"));
		}
		if(StringUtils.isNotBlank(model.getE_sex())){
			dc.add(Restrictions.eq("e_sex", model.getE_sex()));
		}
		if(p_id != 0){
			if(p_id == -1){
				dc.add(Restrictions.eq("e_is", 0));
			}else{
				dc.add(Restrictions.eq("dept.p_id", p_id));
			}
		}
		PageBean pb = empService.getPageBean(dc,page,limit);
		ObjectToJson(pb,new String[]{"cutterPage","pageSize","start"});
		return NONE;
	}
}
